package net.chengp.ms.auth.exception;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import net.chengp.ms.auth.constant.AuthConstant;
import net.chengp.ms.commons.result.R;
import net.chengp.ms.commons.result.Result;

public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws ServletException, IOException {
		Result<?> result = R.error(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
		response.setContentType(AuthConstant.APPLICATION_JSON_UTF8_VALUE);
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		response.getOutputStream().write(JSONObject.toJSONBytes(result, SerializerFeature.WriteMapNullValue));
	}
}